package com.lineage.server.clientpackets;

import com.lineage.echo.ClientExecutor;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Clan;
import com.lineage.server.serverpackets.S_PacketBox;
import com.lineage.server.serverpackets.S_Pledge;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.WorldClan;
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_Pledge extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_Pledge.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      if (pc.getClanid() > 0) {
        L1Clan clan = WorldClan.get().getClan(pc.getClanname());
        pc.sendPackets((ServerBasePacket)new S_Pledge(clan.getClanId()));
        ArrayList<String> page1 = new ArrayList<>();
        ArrayList<String> page2 = new ArrayList<>();
        ArrayList<String> page3 = new ArrayList<>();
        String[] members = clan.getAllMembers();
        int div = 0;
        try {
          div = members.length / 127;
        } catch (Exception exception) {}
        if (div > 0) {
          int i = 0;
          while (i < members.length) {
            if (i < 127) {
              page1.add(members[i]);
            } else if (i < 256) {
              page2.add(members[i]);
            } else {
              page3.add(members[i]);
            } 
            i++;
          } 
          if (page3.size() > 0) {
            div = 3;
          } else if (page2.size() > 0) {
            div = 2;
          } else {
            div = 1;
          } 
          if (page1.size() > 0)
            pc.sendPackets((ServerBasePacket)new S_Pledge(div, 0, page1)); 
          if (page2.size() > 0)
            pc.sendPackets((ServerBasePacket)new S_Pledge(div, 1, page2)); 
          if (page3.size() > 0)
            pc.sendPackets((ServerBasePacket)new S_Pledge(div, 2, page3)); 
          page1.clear();
          page2.clear();
          page3.clear();
          page1 = null;
          page2 = null;
          page3 = null;
        } else {
          int i = 0;
          while (i < members.length) {
            page1.add(members[i]);
            i++;
          } 
          pc.sendPackets((ServerBasePacket)new S_Pledge(1, 0, page1));
        } 
        if (pc.isCrown() && pc.getId() == clan.getLeaderId()) {
          pc.sendPackets((ServerBasePacket)new S_PacketBox(171, (Object[])clan.getOnlineClanMember()));
        } else {
          pc.sendPackets((ServerBasePacket)new S_PacketBox(171, (Object[])clan.getOnlineClanMember()));
        } 
      } else {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1064));
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
